import { IFeidaoAiWeappPage } from '../../../../interfaces';

const FD_EVENTS_WX_RECORD_EROR = 'fd-events-wx-record-error';
const FD_EVENTS_WX_RECORD_FRAME = 'fd-events-wx-record-frame';
const FD_EVENTS_WX_RECORD_PAUSE = 'fd-events-wx-record-pause';
const FD_EVENTS_WX_RECORD_START = 'fd-events-wx-record-start';
const FD_EVENTS_WX_RECORD_STOP = 'fd-events-wx-record-stop';

/*
 * @see [description](https://mp.weixin.qq.com/debug/wxadoc/dev/api/getRecorderManager.html)
 */
export default function get_recorder_manager(fd: IFeidaoAiWeappPage) {
	if (fd.data.fd.record_manager) {
		console.warn('recorder manaer has already been created.');
		return fd.data.fd.record_manager;
	} else {
		const ctx = fd.data.fd.record_manager = wx.getRecorderManager();
		ctx.onError((res) => {
			fd.emit(FD_EVENTS_WX_RECORD_EROR, res);
		});
		ctx.onFrameRecorded((res) => {
			fd.emit(FD_EVENTS_WX_RECORD_FRAME, res);
		});
		ctx.onPause(() => {
			fd.emit(FD_EVENTS_WX_RECORD_PAUSE);
		});
		ctx.onStart(() => {
			fd.emit(FD_EVENTS_WX_RECORD_START);
		});
		ctx.onStop((data) => {
			fd.emit(FD_EVENTS_WX_RECORD_STOP, data);
		});
		return ctx;
	}
}
